package com.lzt.flow.slot;

import com.lzt.flow.entity.Node;
import lombok.Getter;
import lombok.Setter;

import java.util.Iterator;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Getter
@Setter
public class FlowContext {

    private Long predictionId;
    private Long flowId;
    private Node rootNode;
    private Map<String, Object> paramMap;
    private Map<String, List<Map<String, String>>> data;

    public FlowContext() {
        data = new LinkedHashMap<>();
        paramMap = new LinkedHashMap<>();
    }

    public Node getCmpNode(String nodeId) {
        return findNode(rootNode, nodeId);
    }

    private Node findNode(Node startNode, String nodeId) {
        if (startNode == null) {
            return null;
        }
        if (startNode.getId().equals(nodeId)) {
            return startNode;
        }
        if (startNode.getNextNodeList().size() != 0) {
            Iterator<Node> iterator = startNode.getNextNodeList().iterator();
            while (iterator.hasNext()) {
                Node nextNode = iterator.next();
                if (nextNode.getId().equals(nodeId)) {
                    return nextNode;
                }
                Node node = findNode(nextNode, nodeId);
                if (node != null) {
                    return node;
                }
            }
        }
        return null;
    }
}
